package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
public class EmployeeController {


    @Autowired
    private EmployeService employeService;

    @Autowired
    private HttpServletRequest request;

    // 用户登陆
    @PostMapping("/login")
    public R login(@RequestBody Employee employee) {
        R r = employeService.login(employee);
        //  根据 code 判断是否登陆成功 登陆成功则保存 id 到浏览器session
        if (r.getCode() == 1) {
            HttpSession session = request.getSession();
            session.setAttribute("employee", ((Employee) r.getData()).getId());
        }
        return r;
    }

    //  用户退出
    @PostMapping("/logout")
    public R logout() {
        HttpSession session = request.getSession();
        session.invalidate();   //  退出之后销毁浏览器中存储的 id Session
        return R.success("退出成功");
    }

    //  添加员工
    @PostMapping()
    public R add(@RequestBody Employee employee) {
        // 判断 传来的参数
        if (employee == null) {
            return R.error("参数为空");
        }
        //  判断 传来的用户名是否已存在
        LambdaQueryWrapper<Employee> warp = new LambdaQueryWrapper<>();
        warp.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeService.getOne(warp);
        if (one != null) {
            return R.error("用户名已存在 ");
        }
        // 设置一个初始的 md5 格式密码
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        // 修改时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        // 修改人  从session 中获取
//        HttpSession session = request.getSession();
//        Long loginId = (Long) session.getAttribute("employee");
//        if (loginId == null) {
//            return R.error("请登陆后进行");
//        }
//        employee.setCreateUser(loginId);
//        employee.setUpdateUser(loginId);
        employeService.save(employee);
        return R.success("添加成功");
    }

    //   员工分页查询
    @GetMapping("/page")
    public R findPage(Integer page, Integer pageSize, String name) {
        //      int i = 1 / 0;
        R r = employeService.findPage(page, pageSize, name);
        return r;
    }

    //  账号禁用 启用  以及修改员工信息
    @PutMapping()
    public R updateStatus(@RequestBody Employee employee) {
//        HttpSession session = request.getSession();
//        Long loginId = (Long) session.getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(loginId);
        R r = employeService.updateStatus(employee);
        return r;
    }

    //   修改员工信息  回显
    @GetMapping("/{id}")
    public R findById(@PathVariable String id) {
        Employee employee = employeService.getById(id);
        return R.success(employee);
    }
}
